package com.banciyuan.bcywebview.base.factory;

import com.banciyuan.bcywebview.utils.string.StringUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.LinkedList;

import de.greenrobot.daoexample.model.Card;

/**
 * Created by hfxt2008 on 14-8-22.
 * 生成card类的工厂类，用于解析json，然后生成一个list<card>
 */
public class CardFactory {

    public static LinkedList<Card> createCard(JSONArray json) {
        LinkedList<Card> mData = new LinkedList<Card>();
        for (int i = 0; i < json.length(); i++) {
            JSONObject detail = new JSONObject();
            Card data = new Card();

            try {
                detail = (JSONObject) json.get(i);
            } catch (ClassCastException e) {
                try {
                    detail = (JSONObject) ((JSONArray) json.get(i)).get(i);
                } catch (Exception e1) {
                    continue;
                }
            } catch (Exception e) {
                continue;
            }

            data.setTitle(StringUtil.htmlDecoder(StringUtil.getFromJson(detail, "title")));
            data.setUid(StringUtil.getFromJson(detail, "uid"));
            data.setCtime(StringUtil.getFromJson(detail, "ctime"));
            data.setContentAddress(StringUtil.getFromJson(detail, "img_src"));
            data.setVideoNum(StringUtil.getFromJson(detail, "video_num"));
            data.setPicNum(StringUtil.getFromJson(detail, "pic_num"));
            data.setWork(StringUtil.getFromJson(detail, "work"));
            data.setCharacter(StringUtil.getFromJson(detail, "character"));
            data.setRp_id(StringUtil.getFromJson(detail, "rp_id"));
            data.setCp_id(StringUtil.getFromJson(detail, "cp_id"));
            data.setDp_id(StringUtil.getFromJson(detail, "dp_id"));
            data.setUName(StringUtil.getFromJson(detail, "uname"));
            data.setHeadAddress(StringUtil.getFromJson(detail, "avatar"));
            data.setLineType(false);
            mData.add(data);
        }

        return mData;
    }
}
